package com.jlastu.dlxy_androidapp.view.login;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.jlastu.dlxy_androidapp.R;
import com.jlastu.dlxy_androidapp.contract.RegisterContract;
import com.jlastu.dlxy_androidapp.entity.Store;
import com.jlastu.dlxy_androidapp.entity.User;
import com.jlastu.dlxy_androidapp.presenter.RegisterPresenter;
import com.jlastu.dlxy_androidapp.view.MainActivity;

public class RegisterActivity extends AppCompatActivity implements View.OnClickListener, RegisterContract.IRegisterView {
    private RegisterContract.IRegisterPresenter mRegisterPresenter;
    private EditText mUserNameEdit;
    private EditText mPassWordEdit;
    private EditText mPassWordEdit1;

    private Button mRegisterButton;
    private Button mBackButton;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        mRegisterPresenter = new RegisterPresenter(this, this);
        mUserNameEdit = findViewById(R.id.editTextUsername);
        mPassWordEdit = findViewById(R.id.editTextPassword);
        mPassWordEdit1 = findViewById(R.id.editTextPasswordConfirm);
        mRegisterButton = findViewById(R.id.buttonRegister);
        mRegisterButton.setOnClickListener(this);
        mBackButton = findViewById(R.id.buttonBack);
        mBackButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.buttonRegister) {
            String username = mUserNameEdit.getText().toString().trim();
            String password = mPassWordEdit.getText().toString().trim();
            String password1 = mPassWordEdit1.getText().toString().trim();
            Log.d("TAG", "username: "+username);
            Log.d("TAG", "password: "+password);
            Log.d("TAG", "password1: "+password1);
            if (!username.isEmpty() || !password.isEmpty() || !password1.isEmpty()) {
                if (password.equals(password1)) {
                    mRegisterPresenter.getUser(username);
                } else {
                    Toast.makeText(this, "确认密码与密码不相同", Toast.LENGTH_SHORT).show();
                }
            } else {
                Toast.makeText(this, "所有字段不能为空！", Toast.LENGTH_SHORT).show();
            }

        } else if (id == R.id.buttonBack) {
            onBackPressed();
        }

    }

    @Override
    public void showRegister(int code) {
        Log.d("TAG", "showVerify: " + code);
        if (code == 1) {
            Toast.makeText(this, "该用户已存在，请重试", Toast.LENGTH_SHORT).show();
        } else if (code == 2) {
            User user = new User();
            Store store = new Store();
            user.setUserName(mUserNameEdit.getText().toString());
            user.setUserPassword(mPassWordEdit.getText().toString());

            mRegisterPresenter.registerUser(user);
            Toast.makeText(this, "注册成功", Toast.LENGTH_SHORT).show();

        }
    }
}
